 /**
 * jims
 */
 package com.jims.master.modules.sync.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.CacheUtils;
import com.jims.master.common.utils.SecurityUtil;
import com.jims.master.common.utils.SendSms;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.base.user.entity.MasterBaseinfo;
import com.jims.master.modules.base.user.service.MasterBaseinfoService;
import com.jims.master.modules.sync.dao.ClinicMasterDao;
import com.jims.master.modules.sync.entity.ClinicMaster;
import com.jims.master.modules.sync.entity.TransferRecord;
import com.jims.master.modules.sys.entity.OrgDict;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.OrgDictService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


 /**
  * 门诊摘要Service
  * @author ct
  * @version 2017-11-17
  */
 @Service
 @Transactional(readOnly = true)
 public class ClinicMasterService extends CrudService<ClinicMasterDao, ClinicMaster> {

     @Autowired
     private TransferRecordService transferRecordService;
     @Autowired
     private MasterBaseinfoService masterBaseinfoService;

     @Override
     public ClinicMaster get(String id) {
         return super.get(id);
     }

     @Override
     public List<ClinicMaster> findList(ClinicMaster clinicMaster) {
         return super.findList(clinicMaster);
     }

     @Override
     public Page<ClinicMaster> findPage(Page<ClinicMaster> page, ClinicMaster clinicMaster) {
         return super.findPage(page, clinicMaster);
     }
     /*public Page<ClinicMaster> findNoUploadedPage(Page<ClinicMaster> page, ClinicMaster clinicMaster) {
         clinicMaster.setPage(page);
         page.setList(this.dao.findNoUploaded(clinicMaster));
         return page;
     }*/
     @Transactional(readOnly = false)
     public int updateClinin(String  hisPresent,String chiefComplaint,String clinicId) {
         return dao.updateClinin(hisPresent, chiefComplaint, clinicId);
     }

     @Override
     @Transactional(readOnly = false)
     public int save(ClinicMaster clinicMaster) {
         return super.save(clinicMaster);
     }

     @Override
     @Transactional(readOnly = false)
     public int delete(ClinicMaster clinicMaster) {
         return super.delete(clinicMaster);
     }

     public ClinicMaster getByClinicId(String clinicId){
         return dao.getByClinicId(clinicId);
     }
     public ClinicMaster getByClinic(String clinicId){
         return dao.getByClinic(clinicId);
     }

     @Transactional(readOnly = false)
     public int deleteClinic(String id){
         return dao.deleteClinic(id);
     }

     /**
      * 根据病人Id修改门诊就诊记录
      * @param clinicMaster
      * @return
      * @Author
      */
     @Transactional(readOnly = false)
     public int updateClinicType(ClinicMaster clinicMaster) {
         return dao.updateClinicType(clinicMaster);
     }
     /**
      * 通过医生和患者身份证号查询患者的开单记录
      * @param doctorId
      * @param patientId
      * @return
      * @Author WangYQ
      */
     public List<ClinicMaster> getByDocPatIdCard(String doctorId,String patientId){ return dao.getByDocPatIdCard(doctorId,patientId);}
     /**
      * 开单保存
      * @param doctorId
      * @param idCard
      * @param mobile
      * @param age
      * @param baseInfo
      * @return
      * @Author WangYQ
      */
     @Transactional(readOnly = false)
     public ClinicMaster mobileBilling(String doctorId,String idCard,String mobile,String age,String baseInfo,String orgId,String name){
         ClinicMaster clinicMaster = new ClinicMaster();
         clinicMaster.setDoctorId(doctorId);
         clinicMaster.setBaseInfo(baseInfo);
         clinicMaster.setPhone(mobile);
         if(StringUtils.isNotBlank(idCard)){
             clinicMaster.setIdCard(idCard);
         }
//         clinicMaster.setIdCard(idCard);
         clinicMaster.setOrgId(orgId);
         clinicMaster.setName(name);
         if(Integer.parseInt(age)>0){
             clinicMaster.setAgeYear(age);
         }else{
             clinicMaster.setAgeMonth(age);
         }
//       移动端开的单默认是在状态为2，门诊病历数据需从his中取
         clinicMaster.setDelFlag("2");
         clinicMaster.setHisType("0");
         //       生成就诊、门诊号
         Date d = new Date();
         SimpleDateFormat vsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         clinicMaster.setvDatetime(new Date());
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
         Integer  no= dao.getMaxNo(orgId);
         if(no!=null &&!no.equals("")){
             no=no+1;
         }else{
             no=1;
         }
         clinicMaster.setVisitNo(no + "");
         String clinicNo = "90"+sdf.format(d)+no;
         clinicMaster.setClinicNo(clinicNo);
         this.save(clinicMaster);
         return clinicMaster;
     }

     /**
      * 保存转诊信息
      * @param map
      * @Author ct
      * @return
      */
     @Transactional(readOnly = false)
     public int saveTransferRecord(Map map){
         MasterBaseinfo masterBaseinfo = masterBaseinfoService.get((String) map.get("patientId"));
         ClinicMaster clinicMaster = new ClinicMaster();
         if(map.get("clinicId")!=null&&map.get("clinicId")!=""){
             clinicMaster.setId((String) map.get("clinicId"));
         }
         if(map.get("hisFlag")!=null&&map.get("hisFlag")!=""){
             clinicMaster.setHisFlag((String) map.get("hisFlag"));
         }
         clinicMaster.setBaseInfo((String) map.get("patientId"));
         clinicMaster.setHisType("1");
         clinicMaster.setIdCard(masterBaseinfo.getIdCard());
         clinicMaster.setPhone(masterBaseinfo.getMasterContactinfo().getMobilePhone());
         clinicMaster.setAge(masterBaseinfo.getAge());
         clinicMaster.setOrgId((String) map.get("orgId"));
         clinicMaster.setName(masterBaseinfo.getName());
         clinicMaster.setDoctorId((String) map.get("doctorId"));
         //       生成就诊、门诊号
         Date d = new Date();
         clinicMaster.setvDatetime(d);
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
         Integer  no= dao.getMaxNo((String) map.get("orgId"));
         if(no!=null &&!no.equals("")){
             no=no+1;
         }else{
             no=1;
         }
         clinicMaster.setVisitNo(no+"");
         String clinicNo = "91"+sdf.format(d)+no;
         clinicMaster.setClinicNo(clinicNo);
         if(StringUtils.isNotBlank(masterBaseinfo.getAge())){
             if(Integer.parseInt(masterBaseinfo.getAge())>0){
                 clinicMaster.setAgeYear(masterBaseinfo.getAge());
             }else{
                 clinicMaster.setAgeMonth(masterBaseinfo.getAge());
             }
         }else{
             return 0;
         }
         clinicMaster.setDelFlag("2");
         this.save(clinicMaster);
         TransferRecord transferRecord = new TransferRecord();
         if(map.get("id")!=null&&map.get("id")!=""){
             transferRecord.setId((String) map.get("id"));
         }
         transferRecord.setName(masterBaseinfo.getName());
         transferRecord.setSex(masterBaseinfo.getSex());
         transferRecord.setAge(masterBaseinfo.getAge());
         transferRecord.setIdNo(masterBaseinfo.getIdCard());
         transferRecord.setIdentity("1");
         transferRecord.setChargeType("1");
         transferRecord.setPhone(masterBaseinfo.getMasterContactinfo().getMobilePhone());
         transferRecord.setBaseInfo((String) map.get("patientId"));
         transferRecord.setOrgId((String) map.get("orgId"));
         transferRecord.setSituationType((String) map.get("condition"));
         transferRecord.setCause((String) map.get("reason"));
         transferRecord.setProposal((String) map.get("suggestion"));
         transferRecord.setDoctorId((String) map.get("doctorId"));
         transferRecord.setJiwangshi((String) map.get("jiwangshi"));
         transferRecord.setXianbingshi((String) map.get("xianbingshi"));
         transferRecord.setTreatmentProcess((String) map.get("xianbingshi"));
         transferRecord.setOutClinic(clinicMaster.getId());
         int i = transferRecordService.save(transferRecord);
         return i;
     }
     /**
      * 获取开单号
      * @param
      * @Author WangYQ
      * @return
      */
     public String  getClinicNo() {
         Date d = new Date();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
         String clinicNo = "9"+sdf.format(d)+1;
         int i=dao.isBilling(clinicNo);
         int number=1;
         if(i>0){
             number =(int)CacheUtils.get("number")+1;
         }else {
             CacheUtils.put("number",number);
         }
         String clinicNumber="9"+sdf.format(d)+number;
         CacheUtils.put("number",number);
         return clinicNumber;
     }

     public List<ClinicMaster> getZhuanZheng(String doctorId){
         return dao.getZhuanZheng(doctorId);
     }
     public List<ClinicMaster> getKaiDan(String doctorId){
         return dao.getKaiDan(doctorId);
     }
     @Transactional(readOnly = false)
     public int updatePublic(String state,String id) {
         return dao.updatePublic(state,id);
     }

     /**
      * 获取一段时间内未上传的开单列表
      * @param map
      * @param
      * @return
      * @Author WangYQ
      */
     public List<ClinicMaster> getNoUploadedBilling(Map map){
         ClinicMaster clinicMaster = new ClinicMaster();
         clinicMaster.setHisType((String)map.get("hisType"));
         clinicMaster.setHisFlag((String)map.get("hisFalg"));
         DateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         try {
             Date date=format.parse((String)map.get("createDate"));
             clinicMaster.setCreateDate(date);
             clinicMaster.setDoctorId((String)map.get("doctorId"));
             clinicMaster.setBaseInfo((String)map.get("patientId"));
             return dao.getNoUploadedBilling(clinicMaster);
         } catch (ParseException e) {
             e.printStackTrace();
         }
         return null;
     }
     /**
      * 获取一段时间内未上传的开单列表
      * @param map
      * @param
      * @return
      * @Author WangYQ
      */
     public Page<ClinicMaster> getUploadedBilling(Page<ClinicMaster> page,Map map){
         page.setPageNo(Integer.parseInt((String) map.get("pageNO")));
         page.setPageSize(Integer.parseInt((String) map.get("pageSize")));
         ClinicMaster clinicMaster = new ClinicMaster();
         clinicMaster.setHisType((String)map.get("hisType"));
         clinicMaster.setHisFlag((String)map.get("hisFalg"));
         clinicMaster.setDoctorId((String)map.get("doctorId"));
         clinicMaster.setBaseInfo((String)map.get("patientId"));
         clinicMaster.setPage(page);
         page.setList(this.dao.getUploadedBilling(clinicMaster));
         return page;
     }
 }